Skip to content

Redirect to before-start instead of 500 on invalid session cookie - #155

Merged
iMeriem merged 2 commits into
geeksblabla:masterfrom
makraz:fix/session-check-crash
Nov 28, 2026
Merged

iMeriem merged 2 commits into
geeksblabla:masterfrom
makraz:fix/session-check-crash

Conversation

@makraz

@makraz makraz commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Summary

/survey, /thanks and submitAnswers crashed or returned a server error when the __session cookie was expired, tampered with, revoked, or belonged to a deleted user. verifySessionCookie throws an error in those cases instead of returning false.

 /survey, /thanks
-  verifySessionCookie(cookie)        // throws → 500
-  getUser(uid)                       // throws → 500
-  if (!user) redirect                // never runs
+  if (!getSessionUid(cookies))
+    redirect("/before-start")

 submitAnswers
-  try { verify; getUser; saveAnswers } catch → INTERNAL_SERVER_ERROR
+  uid = getSessionUid(cookies) ?? throw UNAUTHORIZED
+  try { saveAnswers(uid) } catch → INTERNAL_SERVER_ERROR

All session cookie code now lives in one file:

 src/lib/firebase/
   server.ts             # + exports shared `auth`
+  session.ts            # getSessionUid / setSessionCookie
 src/actions/
   init-session.ts       # uses setSessionCookie
   submit-answers.ts     # uses getSessionUid
-  remove-session.ts     # unused
 src/pages/
-  api/init-session.ts   # issued session cookies WITHOUT captcha
-  api/remove-session.ts # unused
  • Revocation check: getSessionUid calls verifySessionCookie(cookie, true), which also rejects revoked sessions and disabled users. It still makes one Firebase call, the same as the old verify + getUser.
  • Cookie options: httpOnly, secure (production only), sameSite: "lax", and maxAge of 5 days, matching the Firebase session.
  • Security: /api/init-session let anyone with an anonymous Firebase token get a session cookie without passing the captcha. It is deleted.

Evidence

  • Before: a request with a bad cookie crashed the page.
    GET /survey   Cookie: __session=garbage
    → verifySessionCookie throws "auth/argument-error" → 500
    
    After: the same request redirects.
    GET /survey   Cookie: __session=garbage
    → getSessionUid → null (logged as warn) → 302 /before-start
    
  • pnpm check (astro check): 0 errors. ESLint: clean on all changed files.

@makraz
makraz force-pushed the fix/session-check-crash branch from 85506d0 to 7730d40 Compare November 28, 2026 15:22
@iMeriem
iMeriem merged commit a2e6b75 into geeksblabla:master Nov 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants